package HBaseDemo.ReadFile;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class RFFHDriver extends Configured implements Tool {
    @Override
    public int run(String[] args) throws Exception {
        //获取默认的配置文件
        Configuration conf = this.getConf();
        conf.set("hbase.zookeeper.quorum", "bigdata211");
        //创建job任务
        Job job = Job.getInstance(conf, this.getClass().getSimpleName());
        //反射
        job.setJarByClass(RFFHDriver.class);
        Path path = new Path("hdfs://bigdata211:9000//input_fruit/fruit");
        FileInputFormat.addInputPath(job, path);

        //设置Mapper
        job.setMapperClass(RFFHMapper.class);
        job.setMapOutputKeyClass(ImmutableBytesWritable.class);
        job.setMapOutputKeyClass(Put.class);

        //设置Reducer

        TableMapReduceUtil.initTableReducerJob("fruit", RFFHReducer.class, job);
        job.setNumReduceTasks(1);

        //执行结果
        boolean isSuccess = job.waitForCompletion(true);
        return  isSuccess?0:1;


    }

    public static void main(String[] args) throws Exception {
        //创建HBase的配置文件获取configuration对象
        Configuration configuration = HBaseConfiguration.create();
        int result = ToolRunner.run(configuration, new RFFHDriver(), args);
        System.out.println("上传数据成功！");
        System.exit(result);

    }
}
